﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CourseScheduling
{
    public partial class Reports : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                initNew();
            }
        }

        private void initNew()
        {
            DataView dv = (DataView)this.Sqlyears.Select(DataSourceSelectArguments.Empty);
            DataTable dt = dv.ToTable();
            //ddlYear.Items.Add("All");
            
            foreach(DataRow row in dt.Rows)
            {
                ddlYear.Items.Add(row[0].ToString());
            }
            
            DataView dv1 = (DataView)this.Sqlterm.Select(DataSourceSelectArguments.Empty);
            DataTable dt1 = dv1.ToTable();
            foreach (DataRow row in dt1.Rows)
            {
                this.ddlterm.Items.Add(row[0].ToString());
            }
        }

        private String createStudentWhere()
        {
            String st = null;
            String yr = null;
            string tm = null;

            if (!(ddlterm.SelectedItem.Text.Equals("All") && ddlYear.SelectedItem.Text.Equals("All")))
            {
                if (!ddlterm.SelectedItem.Text.Equals("All"))
                {
                    tm = " semesterRequest = '" + ddlterm.SelectedItem + "'";
                }
                if (!ddlYear.SelectedItem.Text.Equals("All"))
                {
                    yr = " yearRequest = " + ddlYear.SelectedItem;
                }

                if (tm != null && yr != null)
                {
                    st = " where" + tm + " and" + yr;
                }
                else if (tm != null)
                {
                    st = " where" + tm;
                }
                else
                {
                    st = " where" + yr;
                }
            }
            return st;
        }

        private String createProfWhere()
        {
            String st = null;
            String yr = null;
            string tm = null;

            if (!(ddlterm.SelectedItem.Text.Equals("All") && ddlYear.SelectedItem.Text.Equals("All")))
            {
                if (!ddlterm.SelectedItem.Text.Equals("All"))
                {
                    tm = " Term = '" + ddlterm.SelectedItem + "'";
                }
                if (!ddlYear.SelectedItem.Text.Equals("All"))
                {
                    yr = " Year = " + ddlYear.SelectedItem;
                }

                if (tm != null && yr != null)
                {
                    st = " where" + tm + " and" + yr;
                }
                else if (tm != null)
                {
                    st = " where" + tm;
                }
                else
                {
                    st = "where" + yr;
                }
            }
            return st;
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.gvCourseListing.Visible = false;
            this.gvCourseListing.DataSource = null;

            this.gvDayListing.Visible = false;
            this.gvDayListing.DataSource = null;
            this.gvTimeListing.Visible = false;
            this.gvTimeListing.DataSource = null;
            this.gvFacultyListing.DataSource = null;
            this.gvFacultyListing.Visible = false;
            this.gvStudentListing.DataSource = null;
            this.gvStudentListing.Visible = false;

            if (this.ddlReports.SelectedValue == "4")
            {
                this.sqlCourseListing.SelectCommand = this.sqlCourseListing.SelectCommand +
                   createStudentWhere();

                this.gvCourseListing.DataSource = this.sqlCourseListing;
                this.gvCourseListing.DataBind();
                
                this.gvCourseListing.Visible = true;
            }else if (this.ddlReports.SelectedValue == "0")
            {
                this.SqlDayListing.SelectCommand = this.SqlDayListing.SelectCommand +
                    createStudentWhere();
                this.gvDayListing.DataSource = this.SqlDayListing;
                this.gvDayListing.DataBind();
                this.gvDayListing.Visible = true;
            }else if (this.ddlReports.SelectedValue == "2")
            {
                this.SqlStudentListing.SelectCommand = this.SqlStudentListing.SelectCommand +
                    createStudentWhere() + " order by DateSubmitted";
                this.gvStudentListing.DataSource = this.SqlStudentListing;
                this.gvStudentListing.DataBind();
                this.gvStudentListing.Visible = true;
            }

            else if (this.ddlReports.SelectedValue == "1")
            {
                this.SqlTimeListing.SelectCommand = this.SqlTimeListing.SelectCommand +
                  createStudentWhere();
                this.gvTimeListing.DataSource = this.SqlTimeListing;
                this.gvTimeListing.DataBind();
                this.gvTimeListing.Visible = true;
            }
            else if (this.ddlReports.SelectedValue == "3")
            {
                this.SqlFacultyListing.SelectCommand = this.SqlFacultyListing.SelectCommand +
                   createProfWhere();
                this.gvFacultyListing.DataSource = this.SqlFacultyListing;
                this.gvFacultyListing.DataBind();
                this.gvFacultyListing.Visible = true;
            }
        }

        
    }
}